// pages/Personals/UserTrends/UserTrends.js
//获取应用实例
const app = getApp()
const assetsAddress = app.globalData.global.assetsIp + '/' + app.globalData.global.assetsName;
import { queryPetTrendsInfo } from '../../../api/pet';
import { throttle , checkFile } from '../../../utils/util';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    assetsAddress: assetsAddress,
    page: 0,
    dataList: [],
    loading: true,
    noMore: false,
    loadingFailed: false,
    isReachBottom: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this._queryPetTrendsInfo();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    let _this = this;
    _this.resetData();
    setTimeout(()=>{
      wx.stopPullDownRefresh({
        success: (res) => {
          console.log(res,'刷新成功')
          _this.setData({
            refresher: false
          })
          wx.showToast({
            title: '刷新成功',
            icon: 'none',
          })
        },
      })
    },1500)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: throttle(function(){
    console.log('上拉加载')
    if(this.data.isReachBottom){
      this._queryPetTrendsInfo();
    }
  },500),

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    let userId = wx.getStorageSync('userId') ?? 10;
    return {
      path: '/pages/Welcome/Welcome?scene='+userId,
    }
  },
  _queryPetTrendsInfo(){
    let _this = this;
    queryPetTrendsInfo({page: _this.data.page,isNeedUserId: '1',userId: wx.getStorageSync('userId')})
    .then(res => {
      if(res.success){
        var list = _this.data.dataList;
        if(_this.data.page == 0){
          list = [];
        }
        // var arr = [];
        for (let index = 0; index < res.data.content.length; index++) {
          const item = res.data.content[index];
          if(item.fileUrl && item.fileUrl != 'null' && item.fileUrl != ''){
            item.imageUrl = item.fileUrl.split(',')
            var fileList = []
            for (let j = 0; j < item.imageUrl.length; j++) {
              const img = item.imageUrl[j];
              var types = checkFile(img);
              if(types == 'video'){
                fileList.push({url: img,isVideo: true,deletable: true})
              }else if(types == 'image'){
                fileList.push({url: img,isImage: true,deletable: true})
              }
            }
            item.imageUrl = fileList;
          }else{
            item.imageUrl = []
          }
          if(list.length == 0){
            list.push({
              'date': item.createTime.substring(0,4),
              'list': [item],
            })
          }else{
            if(list[list.length - 1].date == item.createTime.substring(0,4)){
              list[list.length - 1].list.push(item);
            }else{
              list.push({
                'date': item.createTime.substring(0,4),
                'list': [item],
              })
            }
          }
          // arr.push(item);
        }
        // if(_this.data.page == 0){
        //   list = list;
        // }else{
        //   list = list.concat(arr);
        // }
        _this.setData({
          dataList: list,
          total: res.data.totalElements,
          page: _this.data.page+1,
        })
        if(_this.data.page * 10 >= res.data.totalElements){
          _this.setData({
            isReachBottom: false,
            noMore: true,
            loading: false,
            loadingFailed: false,
          })
        }else{
          _this.setData({
            isReachBottom: true,
            noMore: false,
            loading: false,
            loadingFailed: false,
          })
        }
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
  // 页面跳转
  goNext(e){
    let _this = this;
    let type = e.target.dataset.type||e.currentTarget.dataset.type;
    let url = e.target.dataset.name||e.currentTarget.dataset.name;
    let item = e.target.dataset.item||e.currentTarget.dataset.item;
    if(item){
      if(type == 1){
        wx.switchTab({
          url: `${url}`,
        })
      }else{
        wx.navigateTo({
          url: `${url}?item=${JSON.stringify(item)}`,
          events: {
            'updateTrends': function(data){
              console.log(data,'data')
              _this.resetData();
              const eventChannel = _this.getOpenerEventChannel()
              eventChannel.emit('updateTrends',data);
            },
          }
        })
      }
    }else{
      if(type == 1){
        wx.switchTab({
          url: `${url}`,
        })
      }else{
        wx.navigateTo({
          url: `${url}`,
        })
      }
    }
  },
  // 初始化
  resetData(){
    let _this = this;
    _this.setData({
      page: 0,
      isReachBottom: false,
      noMore: false,
      loading: false,
      loadingFailed: false,
    })
    _this._queryPetTrendsInfo();
  },
})